package com.qf.android.huihui.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.qf.android.huihui.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.friends.Wechat;

public class LoginActivity extends AppCompatActivity implements PlatformActionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ShareSDK.initSDK(this);

    }
    ///微信登录
    public void weixinClick(View view) {
weixinLogin();
    }


    //qq登录
    public void qqClick(View view) {
        qqLogin();

    }


    //微博登录
    public void weiboClick(View view) {
    weiboLogin();
    }

    private void weixinLogin() {
        Platform pf=ShareSDK.getPlatform(Wechat.NAME);
        pf.authorize();
        pf.setPlatformActionListener(this);
    }

    private void weiboLogin() {
        Platform pf=ShareSDK.getPlatform(SinaWeibo.NAME);
        pf.authorize();
        pf.setPlatformActionListener(this);
    }

    private void qqLogin() {
        Platform pf=ShareSDK.getPlatform(QQ.NAME);
        pf.authorize();
        pf.setPlatformActionListener(this);
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();

        Intent intent=new Intent(LoginActivity.this,PersonalActivity.class);
        SharedPreferences sp=getSharedPreferences("isLogin",MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean("isLogin",true);
        edit.commit();
        intent.putExtra("nickname","金色的羊毛");
        startActivity(intent);
        finish();
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
        Intent intent=new Intent(LoginActivity.this,PersonalActivity.class);
        startActivity(intent);

    }

    @Override
    public void onCancel(Platform platform, int i) {
        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

          Intent intent=new Intent(LoginActivity.this,PersonalActivity.class);
        startActivity(intent);
    }
}
